//
//  LineColorToolsView.swift
//  SmartPenSwift
//
//  Created by tql on 2021/2/20.
//

import Foundation
import UIKit

class LineColorToolsView : UIView {
    var _penColor : UIColor = UIColor.black

    var penColor : UIColor{
        get{
             return _penColor
        }set{
            
            _penColor = newValue
//            for  sbbtn : UIView in self.subviews {
//                sbbtn.removeFromSuperview()
//            }
//            self .initUI()
        }
        
        
    }
    
    typealias colorChangeBlock = (_ changeColor : UIColor) ->()
    var outBlock : colorChangeBlock?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white;
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initUI() -> Void {

        let width : CGFloat = 40
        let gap : CGFloat = 15
        let btnHeight : CGFloat = 50
//        let marginGap =
        var marginGap : CGFloat = ((kScreenWidth) - width * 6 - gap * 5) / 2.0
        let imageArray = ["黑色","红色","蓝色","绿色","黄色","橙色"]
        let imageSelectedArray = ["黑色_selected","红色_selected","蓝色_selected",
        "绿色_selected","黄色_selected","橙色_selected"]
        
        for i : Int in 0..<imageArray.count {
            let  btn  = self.makeButtonWithNormalImage(imageArray[i], imageSelectedArray[i], nil)
            btn.frame = CGRect.init(x: marginGap, y: 10, width: width, height: btnHeight)
            marginGap += gap + width
            btn.tag = i + 1
            self.addSubview(btn)
        }

    }
    @objc func unselectAllBtn(_ btn : UIButton ){
        let btns  = self.subviews
        for  btnin :  UIView  in btns {
            if btnin .isKind(of: UIButton.classForCoder()) {
                if btn .isEqual(btnin) {
                    continue
                }
                else{
                    btn.isSelected = false
                }
            }else{
                continue
            }
            
        }
        
    }
    @objc func btnclick(_ btn : UIButton) -> Void {
        unselectAllBtn(btn)
        if btn.isSelected == true {
            return
        }
        btn.isSelected = !btn.isSelected
        
        if btn.isSelected == true {
            if outBlock != nil {
                
                let colors = [UIColor.customBlack(),UIColor.customRed(),UIColor.customBlue(),UIColor.customGreen(),UIColor.customYellow(),UIColor.customOrange()]
                
                outBlock!( colors[ btn.tag-1])
            }
        }
    }
    func makeButtonWithNormalImage(_ normalImage : String?,_ selectImage : String?, _ title : String? ) -> UIButton {
        let btn = UIButton.init()
        if normalImage != nil {
            btn.setImage(UIImage.init(named: normalImage!), for: .normal)
        }
        if selectImage != nil {
            btn.setImage(UIImage.init(named: selectImage!), for: .selected)
        }
        
        if title != nil {
            btn.setTitle(title!, for: .normal)
        }
        btn.addTarget(self, action: #selector(btnclick(_:)), for: .touchUpInside)
        return btn
    }
    
    
}
